package com.ruoyi.business.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.common.Result;
import com.ruoyi.business.entity.Comments;
import com.ruoyi.business.entity.Orders;
import com.ruoyi.business.entity.Seller;
import com.ruoyi.business.entity.User;
import com.ruoyi.business.entity.dto.CommentsDTO;
import com.ruoyi.business.service.FrontCommentsService;
import com.ruoyi.business.service.FrontOrdersService;
import com.ruoyi.business.service.FrontSellerService;
import com.ruoyi.business.service.FrontUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ao
 * @since 2024-01-06
 */
@RestController
@RequestMapping("/business/comments")
public class FrontCommentsController {
    @Autowired
    private FrontCommentsService commentsService;


    @Autowired
    private FrontUserService userService;


    @Autowired
    private FrontOrdersService ordersService;

    /**
     * 民宿的评论接口
     * @param id
     * @return
     */
    @GetMapping("/getList/{id}")
    public Result<List<CommentsDTO>> getList(@PathVariable("id") Long id){
        LambdaQueryWrapper<Comments> l1=new LambdaQueryWrapper<>();
        l1.eq(Comments::getHomestayId,id);
        List<Comments> list = commentsService.list(l1);

        List<CommentsDTO> result=new ArrayList<>();
        for (Comments comments : list) {
            CommentsDTO commentsDTO = new CommentsDTO();
            BeanUtil.copyProperties(comments,commentsDTO);

            //找到评论用户
            String userid = comments.getUserid();
            LambdaQueryWrapper<User> l2=new LambdaQueryWrapper<User>();
            l2.eq(User::getUserid,userid);
            User one = userService.getOne(l2);
            commentsDTO.setUser(one);

            //找到评论的订单
            LambdaQueryWrapper<Orders> l3=new LambdaQueryWrapper<>();
            l3.eq(Orders::getId,comments.getObjectId());
            Orders one1 = ordersService.getOne(l3);
            commentsDTO.setOrder(one1);

            result.add(commentsDTO);
        }
        return Result.success(result);
    }


    @Autowired
    private FrontSellerService sellerService;
    /**
     * 查看回复
     * @param id
     * @return
     */
    @GetMapping("/selectReply")
    public Result<List<CommentsDTO>> selectReply(Long id){
        LambdaQueryWrapper<Comments> l1=new LambdaQueryWrapper<>();
        l1.eq(Comments::getParentDiscussId,id);

        List<Comments> commentsList = commentsService.list(l1);

        List<CommentsDTO> result=new ArrayList<>();
        for (Comments comments : commentsList) {
            CommentsDTO commentsDTO = new CommentsDTO();
            BeanUtil.copyProperties(comments,commentsDTO);
            //找到评论用户
            String userid = comments.getUserid();
            if(comments.getUserType()==0){//用户评论
                LambdaQueryWrapper<User> l2=new LambdaQueryWrapper<User>();
                l2.eq(User::getUserid,userid);
                User one = userService.getOne(l2);
                commentsDTO.setUser(one);
            }else if(comments.getUserType()==1){//商人评论
                LambdaQueryWrapper<Seller> l2=new LambdaQueryWrapper<Seller>();
                l2.eq(Seller::getUserid,userid);
                Seller one = sellerService.getOne(l2);
                commentsDTO.setSeller(one);
            }

            result.add(commentsDTO);


        }

        return Result.success(result);
    }

    @GetMapping("/list")
    public List<Comments> list(){
        List<Comments> list = commentsService.list();

        return list;
    }



}

